package com.skyable.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skyable.account.entity.SysCompany;
import com.skyable.account.mapper.CompanyMapper;
import com.skyable.account.service.ICompanyService;
import com.skyable.common.common.ServiceResult;
import com.skyable.common.constants.exception.AccountExcpConstants;
import com.skyable.common.constants.param.CommonParams;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author GaoYuan
 * @title: CompanyServiceImpl
 * @projectName private-cloud
 * @description: TODO
 * @date 2021/6/2921:21
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper,SysCompany> implements ICompanyService {
    @Override
    public ServiceResult<SysCompany> insertCompany(SysCompany sysCompany) {
        ServiceResult<SysCompany> serviceResult = new ServiceResult<>();
        SysCompany company = lambdaQuery().eq(SysCompany::getCompanyName, sysCompany.getCompanyName()).one();
        if(company != null) {
            serviceResult.setError(AccountExcpConstants.ERROR_CODE_COMPANY_EXISTS,AccountExcpConstants.ERROR_MSG_COMPANY_EXISTS);
            return serviceResult;
        }
        baseMapper.insert(sysCompany);
        serviceResult.setResult(sysCompany);
        return serviceResult;
    }

    @Override
    public ServiceResult<SysCompany> updateCompany(SysCompany sysCompany) {
        ServiceResult<SysCompany> serviceResult = new ServiceResult<>();
        SysCompany company = lambdaQuery().ne(SysCompany::getId, sysCompany.getId()).eq(SysCompany::getCompanyName, sysCompany.getCompanyName()).one();
        if(company != null) {
            serviceResult.setError(AccountExcpConstants.ERROR_CODE_COMPANY_EXISTS,AccountExcpConstants.ERROR_MSG_COMPANY_EXISTS);
            return serviceResult;
        }
        baseMapper.updateById(sysCompany);
        serviceResult.setResult(sysCompany);
        return serviceResult;
    }

    @Override
    public ServiceResult<IPage<SysCompany>> findCompanyListByPage(int page, int limit, SysCompany sysCompany) {
        ServiceResult<IPage<SysCompany>> serviceResult = new ServiceResult<>();
        Page<SysCompany> sysCompanyPage = new Page<>(page, limit);
        QueryWrapper<SysCompany> queryWrapper = new QueryWrapper<>();
        //需要查询未停用的部门
        queryWrapper.setEntity(sysCompany);
        queryWrapper.orderByAsc(CommonParams.ORDER_CREATE_TIME);
        IPage<SysCompany> sysCompanyIPage = baseMapper.selectPage(sysCompanyPage, queryWrapper);
        serviceResult.setResult(sysCompanyIPage);
        return serviceResult;
    }

    @Override
    public ServiceResult<List<SysCompany>> findCompanyList() {
        ServiceResult<List<SysCompany>> serviceResult = new ServiceResult<>();
        List<SysCompany> sysCompanyList = lambdaQuery().list();
        serviceResult.setResult(sysCompanyList);
        return serviceResult;
    }

    @Override
    public ServiceResult<SysCompany> findCompanyById(Long companyId) {
        ServiceResult<SysCompany> serviceResult = new ServiceResult<>();
        SysCompany sysCompany = lambdaQuery().eq(SysCompany::getId, companyId).one();
        serviceResult.setResult(sysCompany);
        return serviceResult;
    }

    @Override
    public ServiceResult<Integer> deleteCompanyById(Long companyId) {
        ServiceResult<Integer> serviceResult = new ServiceResult<>();
        Integer result = baseMapper.deleteById(companyId);
        serviceResult.setResult(result);
        return serviceResult;
    }
}
